package com.xjj.core.modules.orm;

import java.util.List;

import org.apache.commons.lang.StringUtils;

/**
 * 与具体ORM实现无关的分页查询结果封装,并继承PageRequest中的分页查询参数.
 * 
 * @param <T> Page中记录的类型.
 * 
 * @author calvin
 */
public class Page<T> extends PageRequest {
	private List<T> result = null;
	private int totalCount = -1;
	
	public static final int DEF_COUNT = 20;
	
	private int totalPages;
	//专门为商品设置，最多只有99页的数据
	private int tbTotalPages;

	// 构造函数

	public Page() {
	}

	public Page(final int pageSize) {
		super(pageSize);
	}

	public Page(final int pageSize, final boolean autoCount) {
		super(pageSize, autoCount);
	}
	
	//增加下面这个构造函数---来自jeecms
	public Page(final int pageNo,final int pageSize,final int totalCount){
		this.pageNo = pageNo;
		this.pageSize = pageSize;
		this.totalCount = totalCount;
	}
	
	@SuppressWarnings("unchecked")
	public Page(int pageNo, int pageSize, int totalCount, List list) {
		if (totalCount <= 0) {
			this.totalCount = 0;
		} else {
			this.totalCount = totalCount;
		}
		if (pageSize <= 0) {
			this.pageSize = DEF_COUNT;
		} else {
			this.pageSize = pageSize;
		}
		if (pageNo <= 0) {
			this.pageNo = 1;
		} else {
			this.pageNo = pageNo;
		}
		if ((this.pageNo - 1) * this.pageSize >= totalCount) {
			this.pageNo = totalCount / pageSize;
		}
		this.result = list;
	}


	/**
	 * 取得页内的记录列表.
	 */
	public List<T> getResult() {
		return result;
	}

	public void setResult(final List<T> result) {
		this.result = result;
	}

	/**
	 * 取得总记录数.
	 */
	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(final int totalCount) {
		this.totalCount = totalCount;
	}

	/**
	 * 计算总页数.
	 */
	public int getTotalPages() {
		if (totalCount == -1)
			return -1;

		totalPages = totalCount / pageSize;
		if (totalCount % pageSize > 0) {
			totalPages++;
		}
		return totalPages;
	}
	

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}
	//淘宝客分页使用
	public int getTbTotalPages() {
		if (totalCount == -1)
			return 0;

		tbTotalPages = totalCount / pageSize;
		if (totalCount % pageSize > 0) {
			tbTotalPages++;
		}
		if (tbTotalPages>99) {
			tbTotalPages = 99;
		}
		return tbTotalPages;
	}

	public void setTbTotalPages(int tbTotalPages) {
		this.tbTotalPages = tbTotalPages;
	}

	/**
	 * 取得倒转的排序方向.
	 */
	public String getInverseOrder() {
		String[] orders = StringUtils.split(order, ',');

		for (int i = 0; i < orders.length; i++) {
			if (DESC.equals(orders[i])) {
				orders[i] = ASC;
			} else {
				orders[i] = DESC;
			}
		}
		return StringUtils.join(orders);
	}

	/**
	 * 是否还有下一页.
	 */
	public boolean isHasNext() {
		return (pageNo + 1 <= getTotalPages());
	}
	
	public boolean isFirstPage() {
		return pageNo <= 1;
	}

	public boolean isLastPage() {
		return pageNo >= getTotalPages();
	}

	/**
	 * 取得下页的页号,序号从1开始.
	 */
	public int getNextPage() {
		if (isHasNext())
			return pageNo + 1;
		else
			return pageNo;
	}
	

	/**
	 * 是否还有上一页. 
	 */
	public boolean isHasPre() {
		return (pageNo - 1 >= 1);
	}

	/**
	 * 取得上页的页号,序号从1开始.
	 */
	public int getPrePage() {
		if (isHasPre())
			return pageNo - 1;
		else
			return pageNo;
	}

	
}
